import { finApprovalApi } from "@/api/financialManagement/singleOrderApproval";
import { deleteChargeCostApi } from "@/api/waybillManagement/businessWaybill/waybillQuotation";
import { Modal, message } from "ant-design-vue";
import { Ref, createVNode } from "vue";
import { ExclamationCircleOutlined } from "@ant-design/icons-vue";
export default function OperationHook(state: {
  drawerOpts: Base.DrawerOpts;
  passData: Ref<SingleOrderApproval.ListRows>;
  dialogOpts: Base.DialogOpts;
  formState: Ref<any>;
  emit: any;
  init: (id: string, selectNext?: boolean, search?: any) => void;
  loading: Ref<boolean>;
  search: Ref<SingleOrderApproval.SearchParams>;
}) {
  const { drawerOpts, dialogOpts, passData, formState, emit, init, loading, search } = state;
  // 查看发票
  const showInvoiceHandle = () => {
    drawerOpts.width = "1200px";
    drawerOpts.title = "查看发票";
    drawerOpts.type = 1;
    drawerOpts.passData = formState.value.bizPreOrderShipmentResDto;
    drawerOpts.component = "ShowInvoice";
    drawerOpts.visible = true;
  };
  // 修改调整单价
  const changeAdjustUnitPriceHandle = () => {
    if (formState.value.freightCost.opType === "1") {
      formState.value.freightCost.clearingUnitPrice = formState.value.freightCost.unitPrice + formState.value.freightCost.adjustUnitPrice;
    } else {
      formState.value.freightCost.clearingUnitPrice = formState.value.freightCost.unitPrice - formState.value.freightCost.adjustUnitPrice;
    }
  };
  // 修改客户备注
  const modifyRemarkHandle = () => {
    dialogOpts.title = `修改客户备注`;
    dialogOpts.width = "520px";
    dialogOpts.type = 1;
    dialogOpts.component = "ModifyRemark";
    dialogOpts.passData = formState.value;
    dialogOpts.visible = true;
  };
  // 修改内部备注
  const modifyInnerRemarkHandle = () => {
    dialogOpts.title = `修改内部备注`;
    dialogOpts.width = "520px";
    dialogOpts.type = 2;
    dialogOpts.component = "ModifyRemark";
    dialogOpts.passData = formState.value;
    dialogOpts.visible = true;
  };
  // 查看材积明细
  const showBoxInfoHandle = () => {
    drawerOpts.width = "1200px";
    drawerOpts.title = "查看材积明细";
    drawerOpts.type = 1;
    drawerOpts.passData = passData.value;
    drawerOpts.component = "BoxInfo";
    drawerOpts.visible = true;
  };
  // 新增追加费用
  const addChargeHandle = () => {
    dialogOpts.width = "520px";
    dialogOpts.title = "追加费用";
    dialogOpts.type = 1;
    dialogOpts.passData = formState.value.bizPreOrderShipmentResDto;
    dialogOpts.component = "AddChargeFee";
    dialogOpts.visible = true;
  };
  // 通过并下一个
  const resolveHandle = () => {
    console.log(search.value);
    loading.value = true;
    const params: Parameters<typeof finApprovalApi>[0] = {
      ids: [formState.value.bizOrderShipmentQuotation.id],
      state: 5,
      refuseType: undefined,
      refuseReason: undefined
    };
    return finApprovalApi(params)
      .then((res: any) => {
        const { code } = res;
        if (code === 200) {
          message.success("审批通过成功!");
          init(formState.value.bizOrderShipmentQuotation.id, true, search.value);
        }
      })
      .finally(() => (loading.value = false));
  };
  // 驳回
  const rejectHandle = () => {
    dialogOpts.title = `审批驳回`;
    dialogOpts.width = "520px";
    dialogOpts.type = 1;
    dialogOpts.component = "RejectReason";
    dialogOpts.passData = formState.value.bizOrderShipmentQuotation;
    dialogOpts.visible = true;
  };
  // 弹窗关闭回调
  const dialogSubmitHandle = (type?: number) => {
    dialogOpts.visible = false;
    if (type) {
      // 驳回
      if (type === 1) {
        emit("reject");
      } else {
        init(formState.value.bizOrderShipmentQuotation.id, true, search.value);
      }
    } else {
      init(formState.value.bizOrderShipmentQuotation.id, false, search.value);
    }
  };

  // 删除附加费
  const deleteChargeCostHandle = (row: Base.GeneraDataType) => {
    Modal.confirm({
      title: "是否删除当前附加费?",
      icon: createVNode(ExclamationCircleOutlined),
      onOk() {
        deleteChargeCostApi({ id: row.id }).then((res: any) => {
          const { code } = res;
          if (code === 200) {
            message.success("删除成功!");
            init(passData.value.id);
          }
        });
      }
    });
  };
  // 编辑附加费
  const editChargeHandle = (row: Base.GeneraDataType) => {
    dialogOpts.width = "520px";
    dialogOpts.title = "编辑附加费";
    dialogOpts.type = 2;
    dialogOpts.passData = { waybillNum: passData.value.waybillNum, ...row };
    dialogOpts.component = "AdditionalCharge";
    dialogOpts.visible = true;
  };

  return {
    showInvoiceHandle,
    changeAdjustUnitPriceHandle,
    modifyRemarkHandle,
    modifyInnerRemarkHandle,
    showBoxInfoHandle,
    addChargeHandle,
    dialogSubmitHandle,
    rejectHandle,
    resolveHandle,
    deleteChargeCostHandle,
    editChargeHandle
  };
}
